import {onLoad, onShow} from "@dcloudio/uni-app";
import {getConfigAPI} from "../../services/me";
import {ref} from "vue";
import {useUserInfoStore} from "../../stores/modules/userInfo";
import {BaseImgUrl} from "../../utils/lshttp";
import {goldRedeemAPI, navTtzkAPI, redeemAPI} from "../../services";
import {toPageUtils} from "../../utils/utils";

export const useFuli = () => {
    // 用户信息
    const userInfoStore = useUserInfoStore()
    // 任务完成弹框
    const okShow = ref(false)
    // 会员卡金币兑换列表
    const redeemList = ref([])
    // 天天折扣里列表
    const resTTzkList = ref([])

    // 我的金币
    const config = ref({
        week_member_gold_coin: 0,
        month_member_gold_coin: 0,
        my_gold_coin: 0
    })
    onLoad(async () => {

    })
    onShow(async () => {
        if (userInfoStore.isLoginBool()) {
            await getTaskInfo()
            if (redeemList.value.length === 0) {
                const res = await goldRedeemAPI(-1)
                redeemList.value = res.result
            }
            if (resTTzkList.value.length === 0) {
                const resTTzk = await navTtzkAPI()
                resTTzkList.value = resTTzk.result
            }

        }
    })
    // 获取金币信息
    const getTaskInfo = async () => {
        const configRes = await getConfigAPI()
        config.value = configRes.result
    }

    // 金币明细
    const handleDetails = () => {
        uni.navigateTo({
            url: '/pages/member/golddetails?my_gold_coin=' + config.value.my_gold_coin
        })
    }

    // 去完成
    const mianfeiling = (item) => {
        if (item.tabPage === 1) {
            uni.switchTab({
                url: item.toPath
            })
            return
        }
        uni.navigateTo({
            url: item.toPath
        })
    }

    // 返回
    const okBack = async () => {
        if (platform.value) {
            await uni.switchTab({
                url: '/pages/index/index',
            });
        } else {
            await uni.navigateBack()
        }

    }
    // 跳转金币明细页面
    const toGold = () => {
        uni.navigateTo({
            url: '/pages_gold/index_gold',
        });
    }
    // 任务页面
    const toTask = () => {
        uni.navigateTo({
            url: '/pages_gold/all_task',
        });
    }
    // 兑换会员
    const redeem = async (id) => {
        if (userInfoStore.isLogin()) {
            const res = await redeemAPI(id)
            await getTaskInfo()
            uni.showToast({title: res.msg, icon: 'none', duration: 3000})
        }
    }

    // --------------NEW------------------- 任务列表
    const listItem1 = ref([
        {
            "name": "每日签到领金币",
            "icon": BaseImgUrl + '/fuli/sign.png',
            "btnName": "去签到",
            "subName": "签到一次100金币",
            "toPath": "/pages/signin/signin",
            "tabPage": 0
        },
        // {
        //   "name": "邀请2名新用户各下一单",
        //   "icon": BaseImgUrl + '/once/welfare/get_user.png',
        //   "btnName": "邀请好友",
        //   "subName": "邀请一个有效好友1000金币",
        //   "toPath": "/pages/makemoney/makemoney",
        //   "tabPage": 1
        // },
        // {
        //   "name": "分享给好友",
        //   "icon": BaseImgUrl + '/once/welfare/share.png',
        //   "btnName": "分享给好友",
        //   "subName": "每日分享给好友可得20金币",
        //   "toPath": "/pages/makemoney/makemoney",
        //   "tabPage": 1
        // },
        {
            "name": "邀请领月会员",
            "icon": BaseImgUrl + '/fuli/vip.png',
            "btnName": "邀请好友",
            "subName": "邀请一个有效好友1000金币",
            "toPath": "/pages/makemoney/makemoney",
            "tabPage": 1
        },
        {
            "name": "看视频赚金币",
            "icon": BaseImgUrl + '/images/free/get_member6.png',
            "btnName": "去看视频",
            "subName": "看视频每日可领200金币",
            "toPath": "/pages/makemoney/makemoney",
            "tabPage": 1
        },
        // {
        //   "name": "群内晒图得金币",
        //   "icon": BaseImgUrl + '/once/welfare/share_img.png',
        //   "btnName": "去晒图",
        //   "subName": "参与群内晒图可得100金币",
        //   "toPath": "/pages/makemoney/makemoney",
        //   "tabPage": 1
        // }
    ])
    // 跳转页面
    const toPage = async (item) => {
        await toPageUtils(item)
    }

    // 声明一个名为currentMenuIndex的ref，用于存储当前菜单索引
    const currentMenuIndex = ref(0)
    // 获取当前点击菜单
    const gridChange = (e) => {
        currentMenuIndex.value = e.detail.current;
    }
    // 看广告
    const toAllTaskLookAD = () => {
        // #ifdef MP
        uni.navigateTo({
            url: '/pages_gold/all_task?look=' + true,
        });
        // #endif
        // #ifdef APP-PLUS
        const idApp = 'gh_45626294c5df';
        plus.share.getServices(function (res) {
            const wxServices = res.find(i => i.id.toString() === 'weixin')
            if (wxServices) {
                wxServices.launchMiniProgram({
                    id: idApp,
                    path: "/pages/member/membertask?user_id=" + userInfoStore.userInfo.id + '&platform=APP',
                    // type: 1
                });
            }
        }, function (res) {
            console.log(JSON.stringify(res));
        });
        // #endif
    }
    return {
        userInfoStore,
        okShow,
        redeemList,
        resTTzkList,
        config,
        currentMenuIndex,
        listItem1,
        okBack,
        getTaskInfo,
        handleDetails,
        mianfeiling,
        toGold,
        toTask,
        redeem,
        toPage,
        gridChange,
        toAllTaskLookAD
    }
}